import BaseComponent from "../../Game/Base/BaseClass/BaseComponent";
import FPJDefine from "../../Game/FPJ/Prefabs/FPJDefine";
import { ResourcesLoader } from "./ResourcesLoader";



const { ccclass, property } = cc._decorator;

export type TableItemData = {
	/**
	 * Item下标
	 */
	tableData: any;
	/**
	 * 玩家名字
	 */
	playername: string;
	RoomLevel: number;
	/**
 * 类型 大厅的 和多台里面的
 */
	type: number;
};

@ccclass
export default class TableItemPrefab extends BaseComponent {
	@property(cc.Sprite)
	picSprite: cc.Sprite = null;
	@property(cc.Sprite)
	havepersonSprite: cc.Sprite = null;
	@property(cc.Label)
	tablenumLabel: cc.Label = null;
	@property(cc.SpriteFrame)
	tableSpriteFrame = [];
	@property(cc.SpriteFrame)
	havePersonSpriteFrame = [];
	@property(cc.Node)
	headNode: cc.Node = null;
	@property(cc.Node)
	vistorNode: cc.Node = null;
	@property(cc.Prefab)
	vistorPrefab: cc.Prefab = null;
	@property(cc.Prefab)
	sitplayerPrefab: cc.Prefab = null;
	@property(cc.Node)
	liujiNode: cc.Node = null;

	@property(cc.Node)
	idleNode: cc.Node = null;
	_vistorData: [];
	zuobiao = [
		{ x: 36, y: -150 },
		{ x: 36, y: -192 },
		{ x: 131, y: -150 },
		{ x: 131, y: -192 },
	]
	/**
	 * 是否是展示状态
	 */
	inScrolle: boolean = false;

	private _data: TableItemData = null;

	onLoad() {
		this.node.opacity = 0;
	}
	start() {
		if (this._vistorData && this._vistorData.length > 0) {
			this.vistorNode.active = true;
		} else {
			this.vistorNode.active = false
		}
		if (!this._data.tableData) return;
		this._sethaveperson(this._data.tableData.sitdown_player);
		this.tablenumLabel.string = String(this._data.tableData.tableid);
		this.updataTableData();
	}
	updataTableData() {
		if (!cc.isValid(this.node) || !this._data || !this._data['tableData']) {
			this.node.removeFromParent()
			this.node.destroy();
			return
		};
		this._data.tableData = FPJDefine.gettableDataById(this._data.tableData.table_id)
		//更新坐下的玩家
		if (this._data.tableData == null) {
			this.node.removeFromParent()
			this.node.destroy();
			return
		}
		this._sethaveperson(this._data['tableData']['sitdown_player']);
		this._updataLiuzuo();
		this.updateVistorPlayer();
	}
	_updataLiuzuo() {

	}
	updateVistorPlayer() {
		//更新围观玩家
		// if (this._data.tableData.players && this._data.tableData.players.length > 0) { //更新围观玩家
		// 	this.vistorNode.active = true;
		// 	console.log("更新围观玩家")
		// 	this.vistorNode.children.forEach(element => {
		// 		element.active = false;
		// 	});
		// 	for (var s = 0; s < this._data.tableData.players.length; s++) {
		// 		if (this._data.tableData.players[s] == this._data.tableData.sitdown_player) {
		// 			continue;
		// 		}
		// 		let t = this.vistorNode.children[s]
		// 		if (t) { //刷新
		// 			t.active = true
		// 		} else {
		// 			t = cc.instantiate(this.vistorPrefab);
		// 			t.parent = this.vistorNode;
		// 		}
		// 		cc.vv.GLUtils.getPlayerHeadSprite(this._data.tableData.players.logo || "3", (sf) => {
		// 			t.getChildByName("sp_head").getComponent(cc.Sprite).spriteFrame = sf
		// 		});
		// 	}

		// } else {
		// 	this.vistorNode.active = false;
		// }
	}
	/**
	 * 绑定数据
	 */
	bindData(data: TableItemData) {
		this._data = data;
	}

	/**
	 * 获取数据
	 */
	getData() {
		return this._data;
	}
	/**
 * 获取是否展示状态数据
 */
	getinScrolle() {
		return this.inScrolle;
	}
	/**
	 * 本Item进入ScrollView的时候回调
	 */
	onEnterSrcollView() {
		this.node.opacity = 255;
		this._loadAndShowPic();
		this.inScrolle = true;
		cc.vv.eventCustom.emit("TableItemShow", this._data)
	}

	/**
	 * 本Item离开ScrollView的时候回调
	 */
	onExitScrollView() {
		this.node.opacity = 0;
		this.inScrolle = false;
		cc.vv.eventCustom.emit("TableItemHide", this._data)
	}

	/**
	 * 加载并展示图片
	 */
	private async _loadAndShowPic() {
		this._showPlaceHolder();
		// 模拟延迟一段时间后再加载成功
		await new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve();
			}, 160);
		});
		this.picSprite.spriteFrame = this.tableSpriteFrame[this._data.RoomLevel]
		this._hidePlaceHolder();
	}
	private _sethaveperson(playerid) {
		let index = 0;
		if (playerid) index = 1;
		if (this._data.tableData && this._data.tableData.hold_table && this._data.tableData.hold_table.clear_time
			&& this._data.tableData.hold_table.clear_time * 1000 - cc.vv.GLUtils.getLocalTime() > 0) //如果是留机
		{
			index = 0;
			this.liujiNode.active = true
			if (this._data.tableData.hold_table.rid == cc.vv.session.getRid()) { //自己一个颜色
				this.liujiNode.getChildByName("tip").color = new cc.Color(0xff, 0x37, 0x0)
				this.liujiNode.getChildByName("djsshijian").color = new cc.Color(0xff, 0x37, 0x0)
			} else {
				this.liujiNode.getChildByName("tip").color = new cc.Color(0xff, 0xff, 0xff)
				this.liujiNode.getChildByName("djsshijian").color = new cc.Color(0xff, 0xff, 0xff)
			}

			//创建头像
			if (!this.headNode.children[0]) {
				let p = cc.instantiate(this.sitplayerPrefab);
				p.parent = this.headNode
			}
			this.headNode.active = true;
			cc.vv.GLUtils.getPlayerHeadSprite(this._data.tableData.hold_table.logo, (sf) => {
				this.headNode.children[0].children[0].getChildByName("sp_head").getComponent(cc.Sprite).spriteFrame = sf
			});
			this.headNode.children[0].getChildByName("namebg").getChildByName("name").getComponent(cc.Label).string = this._data.tableData.hold_table.rolename;

			this.idleNode.active = false
			let daojishi = this.liujiNode.getChildByName("djsshijian");
			daojishi.active = true;
			daojishi.runAction(cc.repeatForever(
				cc.sequence(
					cc.delayTime(1),
					cc.callFunc(() => {
						if (this._data.tableData.hold_table.clear_time * 1000 - cc.vv.GLUtils.getLocalTime() <= 0) {
							daojishi.stopAllActions();
							this.idleNode.active = true
							this.liujiNode.active = false
						} else {
							var t = this._data.tableData.hold_table.clear_time * 1000 - cc.vv.GLUtils.getLocalTime();
							var h = Math.floor(t / 1000 / 60 / 60 % 24);
							var m = Math.floor(t / 1000 / 60 % 60);
							var s = Math.floor(t / 1000 % 60);
							var h1 = String(h)
							var m1 = String(m)
							var s1 = String(s)
							if (h < 10) {
								h1 = String("0" + h);
							}
							if (m < 10) {
								m1 = String("0" + m);
							}
							if (s < 10) {
								s1 = String("0" + s);
							}
							daojishi.getComponent(cc.Label).string = h1 + ":" + m1 + ":" + s1;
						}
					}),
				)))
		} else {
			this.idleNode.active = true
			this.liujiNode.active = false
		}
		this.havepersonSprite.spriteFrame = this.havePersonSpriteFrame[index]
		if (playerid) {
			if (!this.headNode.children[0]) {
				let p = cc.instantiate(this.sitplayerPrefab);
				p.parent = this.headNode
			}

			this.headNode.active = true;
			this.idleNode.active = false
			this._setPlayerHeadSprite(playerid)
		} else if (this._data.tableData.hold_table && this._data.tableData.hold_table.clear_time
			&& this._data.tableData.hold_table.clear_time * 1000 - cc.vv.GLUtils.getLocalTime() > 0) {

		}
		else {
			if (this._data.tableData.hold_table && this._data.tableData.hold_table.start_time) {
				this.idleNode.active = false
			} else {
				this.idleNode.active = true
			}
			this.headNode.active = false;
		}
		if (this.liujiNode.active) {
			this.idleNode.active = false;
		}
	}
	private _setPlayerHeadSprite(playerid) {
		let playerdata = FPJDefine.getPlayerById(playerid)
		if (!playerdata) {
			cc.vv.GLUtils.getPlayerHeadSprite("3", (sf) => {
				this.headNode.children[0].children[0].getChildByName("sp_head").getComponent(cc.Sprite).spriteFrame = sf
			});
			this.headNode.children[0].getChildByName("namebg").getChildByName("name").getComponent(cc.Label).string = String("没名字");
			return
		}
		let logo = playerdata.logo || "3";
		cc.vv.GLUtils.getPlayerHeadSprite(logo, (sf) => {
			this.headNode.children[0].children[0].getChildByName("sp_head").getComponent(cc.Sprite).spriteFrame = sf
		});
		this.headNode.children[0].getChildByName("namebg").getChildByName("name").getComponent(cc.Label).string = String(playerdata.rolename);
	}

	private _showPlaceHolder() {
	}

	private _hidePlaceHolder() {
	}

	private onclick() {
		var _gameid = 109;
		let sendEnterTable = () => {
			cc.vv.GLUtils.sendRequest("ATTEnterTable", {
				table_id: this._data.tableData.table_id
			}, (error, data) => {
				if (!error) {
					if (data.errorcode) {
						cc.vv.GLUtils.showTips(data.errordes);
					} else {
						FPJDefine.setTableDetail(data.table_data);
						FPJDefine.setcurrentTableId(data.table_data.table_id)
						FPJDefine.setCurrentState(data.table_data.status)
						if (this._data.type == 1) {
							let _path = cc.vv.GLDefine.getEnglishNameById(_gameid);
							cc.vv.UIMgr.open(cc.vv.GLConfig.waiting_path);
							cc.vv.sceneMgr.openScene(_path, data, () => {
								cc.vv.eventCustom.emit("openGame");
								cc.vv.UIMgr.close(cc.vv.GLConfig.waiting_path, cc.vv.UIMgr.closeType.hide);
							});
						} else {
							cc.vv.eventCustom.emit("resetTable");
						}
					}
				}
				// else {
				// 	cc.vv.GLUtils.showTips(data.errordes);
				// }
			});
		}

		if (this._data.type == 2) {  //走多台  要先发离座子消息
			cc.vv.GLUtils.sendRequest("ATTLeaveTable", {
				table_id: FPJDefine.getcurrentTableId(),   // 桌子ID
			}, (error, data) => {
				if (!data.errorcode) {
					sendEnterTable()
				} else {
					cc.vv.GLUtils.showTips(data.errordes);
				}
			});
		} else {
			sendEnterTable();
		}

	}

}
